$(function () {
    let shoppingCart = new Shopping_cart();
    shoppingCart.getShoppingCart($(".drop-down-menu ul").attr("information-id")).then(res=>{
        $(".shopping-cart-text").text("购物车（"+res+"）")
    })
    $(".levitation-options").on({
        mouseenter: function () {
            // 鼠标进入时执行的操作
            let lastUnderscoreIndex = $(this).children(":first").attr("src").lastIndexOf("_");
            let lastSlice = $(this).children(":first").attr("src").slice(0, lastUnderscoreIndex);
            $(this).children(":first").attr("src", lastSlice + "_%23FF6A01.png")
            $(this).children(":last").css({"color": "#FF6700"})
        },
        mouseleave: function () {
            // 鼠标移出时执行的操作
            let lastUnderscoreIndex = $(this).children(":first").attr("src").lastIndexOf("_");
            let lastSlice = $(this).children(":first").attr("src").slice(0, lastUnderscoreIndex);
            $(this).children(":first").attr("src", lastSlice + "_%23333.png")
            $(this).children(":last").css({"color": "#757575"})
        }
    });

    $(".phone-app").on({
        mouseenter: function () {
            // 鼠标进入时执行的操作
            $(".app-icon").css("display", "block")
        },
        mouseleave: function () {
            // 鼠标移出时执行的操作
            $(".app-icon").css("display", "none")
        }
    });

    $(window).on("scroll", function () {
        let scrollValue = $(window).scrollTop();
        // 在这里编写滚动事件发生时的操作
        if (scrollValue >= 900) {
            $(".top-options").css("display", "block")
        } else {
            $(".top-options").css("display", "none")
        }
    });
    $(".return-top").on("click",function() {
        $("html, body").animate({ scrollTop: 0 }, "slow");
    });

    let name = $(".account-name");
    let timerName ;
    name.on({
        mouseenter: function () {
            // 鼠标进入时执行的操作
            name.css({
                "background-color": "#FFFFFF"
            })
            $(name).children(":last").css({"color": "#FF6700"})
            $(".drop-down-menu").css("display", "block")
            $(".drop-down-menu-img").attr("src", "http://localhost:8083/resource_packs/images/drop_down_color.png")
        }
    }).mouseleave(function () {
        // 使用计时器，延迟隐藏.hidden-content，确保能够移动到.hidden-content上后仍然保持显示状态
        timerName = setTimeout(function () {
            // 鼠标进入时执行的操作
            name.css({
                "background-color": "#333333"
            })
            $(name).children(":last").css({"color": "#b0b0b0"})
            $(".drop-down-menu").css("display", "none")
            $(".drop-down-menu-img").attr("src", "http://localhost:8083/resource_packs/images/drop_down.png")

        }, 300);
    });

    $(".drop-down-menu").on({
        mouseenter: function () {
            clearTimeout(timerName); // 清除计时器
        },
        mouseleave: function () {
            // 鼠标进入时执行的操作
            name.css({
                "background-color": "#333333"
            })
            $(name).children(":last").css({"color": "#b0b0b0"})
            $(".drop-down-menu").css("display", "none")
            $(".drop-down-menu-img").attr("src", "http://localhost:8083/resource_packs/images/drop_down.png")
        }
    });
    // 获取文本框元素
    const searchInput = $('.search-input');
    // 定义数组和索引
    let arr = ["手机",
        "加湿器", "手机换新", "空调", "洗衣机", "冰箱", "电视", "耳机", "加湿器"];

    let index = 0;

    // 更新文本框的值函数
    function updateValue() {
        searchInput.attr("placeholder", arr[index]);

        // 更新索引
        index++;
        if (index >= arr.length) {
            index = 0; // 重置索引，重新开始循环
        }
    }

    // 设置定时器，每隔一段时间调用一次更新值函数
    setInterval(updateValue, 4000);
    // 这里的 2000 表示两秒钟更新一次，你可以根据需要调整时间间隔
    let indexUtil = new Index();
    $(".login-out").on("click", function () {
        indexUtil.logOut().then(function () {
            location.reload();
        })
    })

    let timerCategory; // 定义一个计时器
    let commodityRequest = new Commodity();
    let hiddenContent = $(".hidden-content");
    let commodityTypeUl = $(".commodity-type-ul");
    $('.category').mouseenter(function () {
        if ($(commodityTypeUl).children().length > 0) {
            commodityTypeUl.empty();
        }

        let index = $(this).attr('data-index');
        // 获取当前悬浮的.category的索引
        commodityRequest.navigationBarCommodity(index).then(function (res) {
            let multipleProductsJson = JSON.parse(res);
            for (const index in multipleProductsJson) {
                let html = "<li commodities-id='"+multipleProductsJson[index].commodityId+"'> <a class='basic' href='#'> <img src='http://localhost:8083/resource_packs/images/"+
                    multipleProductsJson[index].commodityImages+"'" +
                    " class='hidden-commodity-img' alt='商品图片'> <div class='information'> " +
                    "<span class='name'>"+multipleProductsJson[index].commodityName+"</span> " +
                    "<p class='price'>"+multipleProductsJson[index].commodityPrice+" 元起</p> </div> </a> </li>";
                let commodity = $(".commodity-type-ul");
                if (index !== multipleProductsJson.length-1+""){
                    let htmlFill = html+"<li><span class='fill'></span></li>";
                    commodity.append(htmlFill)
                }else{
                    commodity.append(html)
                }
                hiddenContent.css({
                    "display":"block"
                });
            }
            //跳转到商品详细
            $(".commodity-type-ul li").on("click",function () {
                let commodityId = $(this).attr("commodities-id");
                if (commodityId !== undefined){
                    console.log(commodityId)
                    let commodityType = $(".commodity-type-ul");
                    let accountId = commodityType.attr("account-id");
                    let name = commodityType.attr("name");
                    //重要信息需要加密
                    let informationId = commodityType.attr("information-id");

                    //加密
                    let aesKeyUtil = new Aes_key_util();
                    const encryptedInformationId = aesKeyUtil.encrypt(informationId);
                    //转格式 转格式是因为如果有特殊符号直接传给后端会变成空格
                    window.open("/www.xiaomi.com/product/details?informationId="+
                        encodeURIComponent(encryptedInformationId)+"&commodityId="+commodityId+"&accountId="+accountId+
                        "&name="+name, "_blank");
                }
            })
        }).catch(function (reason) {
            console.log(reason)
        })
        // 显示对应索引的.hidden-content
    }).mouseleave(function () {
        // 使用计时器，延迟隐藏.hidden-content，确保能够移动到.hidden-content上后仍然保持显示状态
        timerCategory = setTimeout(function () {
            if ($(commodityTypeUl).children().length > 0) {
                commodityTypeUl.empty();
            }
            hiddenContent.css({
                "display":"none"
            })
        }, 300);
    });

    $(".go-personal-center").on("click",function () {
        let menu = $(".drop-down-menu ul");
        let accountId = menu.attr("account-id");
        let name = menu.attr("name");
        //重要信息需要加密
        let informationId = menu.attr("information-id");
        let menuId = $(this).attr("menu");

        //加密
        let aesKeyUtil = new Aes_key_util();
        const encryptedInformationId = aesKeyUtil.encrypt(informationId);
        // 打开一个新窗口 转格式 转格式是因为如果有特殊符号直接传给后端会变成空格
        let newWindow = window.open("/www.xiaomi.com/center?informationId="+
            encodeURIComponent(encryptedInformationId)+"&accountId="+accountId+
            "&menu="+menuId+"&name="+name, "_blank");
        console.log(newWindow)
    })
    $(".go-personal").on("click",function () {
        window.open("/www.xiaomi.com/personal","_blank")
    })
    $(".account-name").on("click",function () {
        let menu = $(".drop-down-menu ul");
        let accountId = menu.attr("account-id");
        let name = menu.attr("name");
        //重要信息需要加密
        let informationId = menu.attr("information-id");

        //加密
        let aesKeyUtil = new Aes_key_util();
        const encryptedInformationId = aesKeyUtil.encrypt(informationId);
        // 打开一个新窗口 转格式 转格式是因为如果有特殊符号直接传给后端会变成空格
        let newWindow = window.open("/www.xiaomi.com/center?informationId="+
            encodeURIComponent(encryptedInformationId)+"&accountId="+accountId+
            "&name="+name, "_blank");

    })
    $(".levitation-shopping-cart").on("click",function () {
        window.open("/www.xiaomi.com/shopping/cart?accountId="+$(this).attr("account-id")
            , "_blank");
    })
    // 监听.hidden-content的mouseenter和mouseleave事件，移动出时清除计时器，确保能够保持显示状态
    $('.hidden-content').mouseenter(function () {
        clearTimeout(timerCategory); // 清除计时器
    }).mouseleave(function () {
        if ($(commodityTypeUl).children().length > 0) {
            commodityTypeUl.empty();
        }
        hiddenContent.css({
            "display":"none"
        })
    });
    $(".search-img").on("click",function () {
        let accountId = $(".account-name").attr("account-id");
        let name = $(".account-name").attr("name");
        let informationId = $(".account-name").attr("id");
        let searchInput = $(".search-input").val();
        //加密
        let aesKeyUtil = new Aes_key_util();
        const encryptedInformationId = aesKeyUtil.encrypt(informationId);
        window.open("/www.xiaomi.com/search?accountId="+accountId+"&name="+name+"&id="+encodeURIComponent(encryptedInformationId)+"&searchInput="+searchInput, "_blank");
    })
});
